package com.tracy.life.discovery.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.tracy.life.R;
import com.tracy.life.bean.HotStrategyBean;
import com.tracy.life.bean.SpecialBean;
import com.tracy.life.constant.UrlString;
import com.tracy.life.discovery.adapter.HotRecyclerViewAdapter;
import com.tracy.life.discovery.prensenter.StrategyHotPresenter;
import com.tracy.life.discovery.view.StrategyHotView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * viewPager 里 攻略碎片
 */
public class StrategyFragment extends Fragment implements StrategyHotView {
    @InjectView(R.id.recycler_view_hot)
    RecyclerView recyclerViewHot;

    private List<String> mUrlList;
    private List<SpecialBean.DataBean.GroupsBean> mList;

    private StrategyHotPresenter presenter;
    private HotRecyclerViewAdapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUrlList = new ArrayList<>();
        mList = new ArrayList<>();
        adapter = new HotRecyclerViewAdapter(getActivity());

        presenter = new StrategyHotPresenter(this);
        presenter.loadDatas();//加载数据
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_strategy, container, false);
        ButterKnife.inject(this, view);

        recyclerViewHot.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerViewHot.setAdapter(adapter);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void loadFailure() {
        Toast.makeText(getActivity(), "加载失败！", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void loadSuccess(List<HotStrategyBean.DataBean.CollectionsBean> collections) {

        lodaDatas(); // 加载查看更多下方列表的数据
        Log.i("123","长度：：：：："+collections.size());
        for (int i = 0; i < 4; i++) {
            Log.i("123","数据：：：：："+collections.get(i).banner_image_url);
            mUrlList.add(collections.get(i).banner_image_url);
        }

    }

    /**
     * 加载查看更多 下方列表的数据
     */
    private void lodaDatas() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(UrlString.URL_SPECIAL).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                final SpecialBean specialBean = new Gson().fromJson(result, SpecialBean.class);
                final List<SpecialBean.DataBean.GroupsBean> list = specialBean.data.channel_groups;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        mList.addAll(list);
                        adapter.setDatas(mUrlList,mList);
                    }
                });
            }
        });

    }

    Handler handler = new Handler();


}
